package com.wz.project.api.sap.controller;

import com.alibaba.fastjson.JSON;
//import com.sun.media.jfxmedia.logging.Logger;
import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.project.api.barcode.domain.MatnrInfo;
import com.wz.project.api.sap.service.SapService;
import com.wz.project.bas.product.service.IProductService;
import com.wz.project.pda.service.PdaService;
import com.wz.project.sap.CUSTOMER.service.IZflWmsGetCustomerService;
import com.wz.project.sap.DN.domain.ZflWmsGetDn;
import com.wz.project.sap.DN.service.IZflWmsGetDnService;
import com.wz.project.sap.DN_POST_DN.controller.ZflWmsDnPostDnController;
import com.wz.project.sap.DN_POST_DN.domain.ZflWmsDnPostDn;
import com.wz.project.sap.DN_POST_DN.domain.ZflWmsDnPostRef;
import com.wz.project.sap.DN_POST_DN.service.IZflWmsDnPostDnService;
import com.wz.project.sap.DN_POST_SERIALNO.controller.ZflWmsDnPostSerialnoController;
import com.wz.project.sap.INSPECT2UNRESTRICTED.service.IZflWmsInspect2unrestrictedService;
import com.wz.project.sap.MATERIAL.domain.ZflWmsGetMaterial;
import com.wz.project.sap.MATERIAL.service.IZflWmsGetMaterialService;
import com.wz.project.sap.MATUNIT.domain.ZflWmsGetMatUnit;
import com.wz.project.sap.MATUNIT.service.IZflWmsGetMatUnitService;
import com.wz.project.sap.POST_TMDATA_RETURNDATA.service.IYppRfcPostTmdataReturndataService;
import com.wz.project.sap.POST_TMDATA_SERIAL.domain.YppRfcPostTmdataSerial;
import com.wz.project.sap.POST_TMDATA_SERIAL.service.IYppRfcPostTmdataSerialService;
import com.wz.project.sap.POST_TMDATA_SERIALAREA.service.IYppRfcPostTmdataSerialareaService;
import com.wz.project.sap.POST_TMDATA_TMINTERF.domain.YppRfcPostTmdataTminterf;
import com.wz.project.sap.POST_TMDATA_TMINTERF.service.IYppRfcPostTmdataTminterfService;
import com.wz.project.sap.PRO_LIST_DNLIST.domain.YppRfcGetWmsProListDnlist;
import com.wz.project.sap.PRO_LIST_DNLIST.service.IYppRfcGetWmsProListDnlistService;
import com.wz.project.sap.PRO_LIST_PRDLIST.domain.YppRfcGetWmsProListPrdlist;
import com.wz.project.sap.PRO_LIST_PRDLIST.service.IYppRfcGetWmsProListPrdlistService;
import com.wz.project.sap.PRO_LIST_PRODTEXT.domain.YppRfcGetWmsProListProdtext;
import com.wz.project.sap.PRO_LIST_PRODTEXT.service.IYppRfcGetWmsProListProdtextService;
import com.wz.project.sap.PYPK.domain.ZflWmsPyPk;
import com.wz.project.sap.PYPK.service.IZflWmsPyPkService;
import com.wz.project.api.sap.domain.InputData;
import com.wz.project.api.sap.sapconn.SAPConn;
import com.wz.project.sap.STOCK_TRANSFER.domain.ZflWmsStockTransferHeader;
import com.wz.project.sap.STOCK_TRANSFER.domain.ZflWmsStockTransferItem;
import com.wz.project.sap.STOCK_TRANSFER.domain.ZflWmsStockTransferSeri;
import com.wz.project.sap.STOCK_TRANSFER.service.ZflWmsStockTransferService;
import com.wz.project.sap.VENDOR.service.IZflWmsGetVendorService;
import com.wz.project.stock.stockdetail.domain.BasStockDetail;
import com.wz.project.stock.stockdetail.service.IBasStockDetailService;
import com.wz.project.util.StringUtil;
import com.wz.project.util.XmlUtil;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/API/SAP")
public class SapController {

    @Autowired
    SapService sapService;
    @Resource
    IZflWmsGetMaterialService zflWmsGetMaterialService;
    @Resource
    IZflWmsGetMatUnitService zflWmsGetMatUnitService;
    @Resource
    IYppRfcPostTmdataSerialService yppRfcPostTmdataSerialService;
    @Resource
    IZflWmsPyPkService zflWmsPyPkService;
    @Resource
    IZflWmsInspect2unrestrictedService zflWmsInspect2unrestrictedService;
    @Resource
    IZflWmsGetDnService zflWmsGetDnService;
    @Resource
    ZflWmsDnPostDnController zflWmsDnPostDnController;
    @Resource
    ZflWmsDnPostSerialnoController zflWmsDnPostSerialnoController;
    @Resource
    IZflWmsGetVendorService zflWmsGetVendorService;
    @Resource
    IZflWmsGetCustomerService zflWmsGetCustomerService;
    @Resource
    IYppRfcGetWmsProListPrdlistService yppRfcGetWmsProListPrdlistService;
    @Resource
    IYppRfcGetWmsProListProdtextService yppRfcGetWmsProListProdtextService;
    @Resource
    IYppRfcGetWmsProListDnlistService yppRfcGetWmsProListDnlistService;
    @Resource
    IYppRfcPostTmdataSerialareaService yppRfcPostTmdataSerialareaService;
    @Resource
    IYppRfcPostTmdataReturndataService yppRfcPostTmdataReturndataService;
    @Resource
    IZflWmsDnPostDnService zflWmsDnPostDnService;
    @Autowired
    IProductService productService;
    @Autowired
    IBasStockDetailService basStockDetailService;
    @Autowired
    private IYppRfcPostTmdataTminterfService yppRfcPostTmdataTminterfService;
    @Autowired
    private ZflWmsStockTransferService zflWmsStockTransferService;
    @Autowired
    PdaService pdaService;

    /**
     * 物料主数据
     *
     * @param MATNR
     * @param BEGDATE
     * @param ENDDATE
     * @return
     */
    @RequestMapping("/ZFL_WMS_GET_MATERIAL")
    @ResponseBody
    public AjaxResult getMaterialMasterData(String WERKS, String MATNR, String BEGDATE, String ENDDATE) {
        AjaxResult ajaxResult = SAPConn.getTableXml("ZFL_WMS_GET_MATERIAL",
                new InputData("IV_WERKS", WERKS),
                new InputData("IV_MATNR", MATNR),
                new InputData("IV_BEGDATE", BEGDATE),
                new InputData("IV_ENDDATE", ENDDATE));
        AjaxResult ajaxResult1 = new AjaxResult();
        if (Integer.parseInt(String.valueOf(ajaxResult.get("code"))) == 500) {
            ajaxResult1.put("msg", "同步未成功" + "," + ajaxResult.get("msg"));
        } else {
            List<ZflWmsGetMatUnit> zflWmsGetMatUnits = (List<ZflWmsGetMatUnit>) getAjaxResult(ajaxResult, ZflWmsGetMatUnit.class, "ET_MARM", "ZFL_WMS_GET_MATERIAL").get("data");

            for (ZflWmsGetMatUnit zflWmsGetMatUnit : zflWmsGetMatUnits) {
                zflWmsGetMatUnitService.deleteZflWmsGetMatUnitById(zflWmsGetMatUnit);
                zflWmsGetMatUnitService.insertZflWmsGetMatUnit(zflWmsGetMatUnit);

            }

            List<ZflWmsGetMaterial> zflWmsGetMaterials = (List<ZflWmsGetMaterial>) getAjaxResult(ajaxResult, ZflWmsGetMaterial.class, "ET_TABLE", "ZFL_WMS_GET_MATERIAL").get("data");
            if (zflWmsGetMaterials != null) {
                for (ZflWmsGetMaterial zflWmsGetMaterial : zflWmsGetMaterials) {
                    if ("2240".equals(zflWmsGetMaterial.getWERKS())) {

                        ZflWmsGetMaterial zflWmsGetMaterial1 = zflWmsGetMaterialService.selectZflWmsGetMaterialById(zflWmsGetMaterial.getMATNR());
                        if (zflWmsGetMaterial1 == null) {
                            zflWmsGetMaterialService.insertZflWmsGetMaterial(zflWmsGetMaterial);
                        } else {
                            zflWmsGetMaterialService.updateZflWmsGetMaterial(zflWmsGetMaterial);
                        }
                        sapService.insertProduct(zflWmsGetMaterial);
                        List<ZflWmsGetMatUnit> zflWmsGetMatUnits1 = zflWmsGetMatUnitService.selectZflWmsGetMatUnitsById(zflWmsGetMaterial.getMATNR());
                        if (zflWmsGetMatUnits1.size() > 1) {
                            ZflWmsGetMatUnit zflWmsGetMatUnit = zflWmsGetMatUnits1.get(0);
                            ZflWmsGetMatUnit zflWmsGetMatUnit1 = zflWmsGetMatUnits1.get(1);
                            if (zflWmsGetMaterial.getMEINS().equals(zflWmsGetMatUnit.getMEINH())) {
                                String qty = String.valueOf(Float.parseFloat(zflWmsGetMatUnit1.getUMREZ()) / Float.parseFloat(zflWmsGetMatUnit1.getUMREN()) / Float.parseFloat(zflWmsGetMatUnit.getUMREZ()) * Float.parseFloat(zflWmsGetMatUnit.getUMREN()));
                                zflWmsGetMatUnit1.setUMREZ(qty);
                                zflWmsGetMatUnitService.updateZflWmsGetMatUnit(zflWmsGetMatUnit1);
                                zflWmsGetMatUnitService.deleteZflWmsGetMatUnitById(zflWmsGetMatUnit);
                            } else {
                                String qty = String.valueOf(Float.parseFloat(zflWmsGetMatUnit.getUMREZ()) / Float.parseFloat(zflWmsGetMatUnit.getUMREN()) / Float.parseFloat(zflWmsGetMatUnit1.getUMREZ()) * Float.parseFloat(zflWmsGetMatUnit1.getUMREN()));
                                zflWmsGetMatUnit.setUMREZ(qty);
                                zflWmsGetMatUnitService.updateZflWmsGetMatUnit(zflWmsGetMatUnit);
                                zflWmsGetMatUnitService.deleteZflWmsGetMatUnitById(zflWmsGetMatUnit1);
                            }
                        }
                    }
                }
            }
            ajaxResult1.put("msg", "同步成功!");
        }
        return ajaxResult1;
    }

//    /**
//     * 供应商主数据
//     *
//     * @param LIFNR
//     * @param BEGDATE
//     * @param ENDDATE
//     * @return
//     */
//    @RequestMapping("/ZFL_WMS_GET_VENDOR")
//    @ResponseBody
//    public AjaxResult getVenDorData(String LIFNR, String BEGDATE, String ENDDATE) {
//        AjaxResult ajaxResult = SAPConn.getTableXml("ZFL_WMS_GET_VENDOR",
//                new InputData("IV_LIFNR", LIFNR),
//                new InputData("IV_BEGDATE", BEGDATE),
//                new InputData("IV_ENDDATE", ENDDATE));
//        List<ZflWmsGetVendor> zflWmsGetVendors = (List<ZflWmsGetVendor>) getAjaxResult(ajaxResult, ZflWmsGetVendor.class, "ET_TABLE").get("data");
//        for (ZflWmsGetVendor zflWmsGetVendor : zflWmsGetVendors) {
//            sapService.insertSupplier(zflWmsGetVendor);
//            zflWmsGetVendorService.insertZflWmsGetVendor(zflWmsGetVendor);
//        }
//        return AjaxResult.success();
//    }
//
//
//    /**
//     * 客户主数据
//     *
//     * @param KUNNR
//     * @param BEGDATE
//     * @param ENDDATE
//     * @return
//     */
//    @RequestMapping("/ZFL_WMS_GET_CUSTOMER")
//    @ResponseBody
//    public AjaxResult getCustomerData(String KUNNR, String BEGDATE, String ENDDATE) {
//        AjaxResult ajaxResult = SAPConn.getTableXml("ZFL_WMS_GET_CUSTOMER",
//                new InputData("IV_KUNNR", KUNNR),
//                new InputData("IV_BEGDATE", BEGDATE),
//                new InputData("IV_ENDDATE", ENDDATE));
//        List<ZflWmsGetCustomer> zflWmsGetCustomers = (List<ZflWmsGetCustomer>) getAjaxResult(ajaxResult, ZflWmsGetCustomer.class, "ET_TABLE").get("data");
//        for (ZflWmsGetCustomer zflWmsGetCustomer : zflWmsGetCustomers) {
//            zflWmsGetCustomerService.insertZflWmsGetCustomer(zflWmsGetCustomer);
//            sapService.insertCustomer(zflWmsGetCustomer);
//        }
//
//        return AjaxResult.success();
//    }
//
//
////    /**
////     * 物料单位转换
////     * @param MATNR
////     * @return
////     */
////    @RequestMapping("/ZFL_WMS_GET_MAT_UNIT")
////    @ResponseBody
////    public AjaxResult getMartUnitData(String MATNR) {
////        AjaxResult ajaxResult = SAPConn.getTableXml("ZFL_WMS_GET_MAT_UNIT",
////                new InputData("IV_MATNR", MATNR));
////        List<ZflWmsGetMatUnit> zflWmsGetMatUnits = (List<ZflWmsGetMatUnit>) getAjaxResult(ajaxResult, ZflWmsGetMatUnit.class, "ET_TABLE").get("data");
////        zflWmsGetMatUnits.forEach(z -> zflWmsGetMatUnitController.addSave(z));
////        return AjaxResult.success();
////    }
//

    /**
     * 库存盘盈盘亏
     *
     * @return
     */
    @RequestMapping("/ZFL_WMS_PY_PK")
    @ResponseBody
    public AjaxResult getPYPKData(@RequestBody String json) {
        ZflWmsPyPk zflWmsPyPk = JSON.parseObject(json, ZflWmsPyPk.class);
        AjaxResult ajaxResult = SAPConn.getPYPKXml(zflWmsPyPk);

        return AjaxResult.success();
    }

    /**
     * 库存调拨
     *
     * @return
     */

    @RequestMapping("/ZFL_WMS_STOCK_TRANSFER")
    @ResponseBody
    public AjaxResult getStockTransfer(ZflWmsStockTransferHeader header, ZflWmsStockTransferItem item, ZflWmsStockTransferSeri seri) {

        List<ZflWmsStockTransferHeader> list1 = zflWmsStockTransferService.getHeader(header);
        List<ZflWmsStockTransferItem> list2 = zflWmsStockTransferService.getItem(item);
        List<ZflWmsStockTransferSeri> list3 = zflWmsStockTransferService.getSeri(seri);

        header = SAPConn.getTransferXml(list1, list2, list3);
        zflWmsStockTransferService.updateHeader(header);
        return AjaxResult.success(header.getEvMessage());
    }

    /**
     * 质检转非限制
     * @param zflWmsDnPostDn1
     * @return
     */
    @RequestMapping("/ZFL_WMS_INSPECT2UNRESTRICTED")
    @ResponseBody
    public void getCheckData(ZflWmsDnPostDn zflWmsDnPostDn1) {

        List<ZflWmsDnPostDn> zflWmsDnPostDnList = zflWmsDnPostDnService.selectZflWmsDnPostDisticnct(zflWmsDnPostDn1.getVBELN());

        for (ZflWmsDnPostDn zflWmsDnPostDn : zflWmsDnPostDnList) {
            AjaxResult ajaxResult = SAPConn.getTableXml("ZFL_WMS_INSPECT2UNRESTRICTED",
                    new InputData("IV_PRUEFLOS", zflWmsDnPostDn.getPRUEFLOS()),
                    new InputData("IV_VCODEGRP", "IQC"),
                    new InputData("IV_VCODE", "0001"),
                    new InputData("IV_TXLINE", "UD决策备注"),
                    new InputData("IV_TYPE", zflWmsDnPostDn.getTYPE())
            );
            if (Integer.parseInt(String.valueOf(ajaxResult.get("code"))) == 500) {
//              zflWmsDnPostDn.setStatus("4");
                zflWmsDnPostDn.setError(String.valueOf(ajaxResult.get("msg")));
                zflWmsDnPostDnService.updateZflWmsDnPostDnZJ(zflWmsDnPostDn);
            } else {
                zflWmsDnPostDn.setStatus("5");
                zflWmsDnPostDn.setError("质检回传成功");
                zflWmsDnPostDnService.updateZflWmsDnPostDnZJ(zflWmsDnPostDn);
            }
        }

//        List<ZflWmsInspect2unrestricted> zflWmsInspect2unrestricteds = (List<ZflWmsInspect2unrestricted>) getAjaxResult(ajaxResult, ZflWmsInspect2unrestricted.class, "IT_SERI", "ZFL_WMS_INSPECT2UNRESTRICTED").get("data");
//        for (ZflWmsInspect2unrestricted zflWmsInspect2unrestricted : zflWmsInspect2unrestricteds) {
//            zflWmsInspect2unrestrictedService.insertZflWmsInspect2unrestricted(zflWmsInspect2unrestricted);
//        }

    }

    /**
     * 内外向交货单
     *
     * @param
     * @return
     */
    @RequestMapping("/ZFL_WMS_GET_DN")
    @Log(title = "内外向交货单获取", businessType = BusinessType.INSERT)
    @ResponseBody
    public AjaxResult getDNData(String VBELN, String BEGDATE, String ENDDATE) {
        AjaxResult ajaxResult = SAPConn.getTableXml("ZFL_WMS_GET_DN",
                new InputData("IV_VBELN", VBELN),
                new InputData("IV_BEGDATE", BEGDATE),
                new InputData("IV_ENDDATE", ENDDATE));
        if (Integer.parseInt(String.valueOf(ajaxResult.get("code"))) == 500) {
            return AjaxResult.error("同步失败" + ajaxResult.get("msg"));
        } else {
            List<ZflWmsGetDn> zflWmsGetDns = (List<ZflWmsGetDn>) getAjaxResult(ajaxResult, ZflWmsGetDn.class, "ET_TABLE", "ZFL_WMS_GET_DN").get("data");
            if (zflWmsGetDns != null) {
                for (ZflWmsGetDn zflWmsGetDn : zflWmsGetDns) {
                    if ("2240".equals(zflWmsGetDn.getWERKS())) {
                        if (zflWmsGetDn.getKUNNR() != null && !"".equals(zflWmsGetDn.getKUNNR())) {
                            List<MatnrInfo> matnrInfoLists = pdaService.selectMatnrInfobd(StringUtil.Stringdel(zflWmsGetDn.getMATNR()));

                            if (matnrInfoLists.size() == 0) {
                                //从v_tm_matnr_minfo表中用母件码查询所有齐套件信息
                                List<MatnrInfo> matnrInfoList = pdaService.selectMatnrInfo(StringUtil.Stringdel(zflWmsGetDn.getMATNR()));
                                if (matnrInfoList.size() > 0) {
                                    for (MatnrInfo m : matnrInfoList) {
                                        pdaService.insetMatnrInfo(m);
                                    }
                                }
                            }
                            List<MatnrInfo> matnrInfoList = null;
                            List<MatnrInfo> matnrInfos = pdaService.selectMatnrInfobd(StringUtil.Stringdel(zflWmsGetDn.getMATNR()));
                            if (matnrInfos.size() == 1) {
                                matnrInfoList = matnrInfos;
                            } else if (matnrInfos.size() > 1) {
                                matnrInfoList = pdaService.selectMatnrInfobd(StringUtil.Stringdel(zflWmsGetDn.getMATNR() + "-"));
                            }

                            sapService.insertOutStockout(zflWmsGetDn, matnrInfoList);
                        } else {
                            List<MatnrInfo> matnrInfoLists = pdaService.selectMatnrInfobd(StringUtil.Stringdel(zflWmsGetDn.getMATNR()));
                            if (matnrInfoLists.size() == 0) {
                                //从v_tm_matnr_minfo表中用母件码查询所有齐套件信息
                                List<MatnrInfo> matnrInfoList = pdaService.selectMatnrInfo(StringUtil.Stringdel(zflWmsGetDn.getMATNR()));
                                if (matnrInfoList.size() > 0) {
                                    for (MatnrInfo m : matnrInfoList) {
                                        pdaService.insetMatnrInfo(m);
                                    }
                                }
                            }
                            List<MatnrInfo> matnrInfoList = null;
                            List<MatnrInfo> matnrInfos = pdaService.selectMatnrInfobd(StringUtil.Stringdel(zflWmsGetDn.getMATNR()));
                            if (matnrInfos.size() == 1) {
                                matnrInfoList = matnrInfos;
                            } else if (matnrInfos.size() > 1) {
                                matnrInfoList = pdaService.selectMatnrInfobd(StringUtil.Stringdel(zflWmsGetDn.getMATNR() + "-"));
                            }
                            sapService.insertInStockinDN(zflWmsGetDn, matnrInfoList);
                        }
                        ZflWmsGetDn zflWmsGetDn1 = new ZflWmsGetDn();
                        zflWmsGetDn1.setVBELN(zflWmsGetDn.getVBELN());
                        zflWmsGetDn1.setMATNR(zflWmsGetDn.getMATNR());
                        zflWmsGetDn1.setPOSNR(zflWmsGetDn.getPOSNR());
                        List<ZflWmsGetDn> zflWmsGetDns1 = zflWmsGetDnService.selectZflWmsGetDnList(zflWmsGetDn1);
                        if (zflWmsGetDns1.size() == 0) {
                            zflWmsGetDnService.insertZflWmsGetDn(zflWmsGetDn);
                        }
                    }
                }
            }
            return AjaxResult.success("同步成功");
        }

    }

    /**
     * 内外向交货单过账
     *
     * @return
     */
    @RequestMapping("/ZFL_WMS_DN_POST")
    @ResponseBody
    public void getDNPostData(List<ZflWmsDnPostDn> dn) {
//        ZflWmsDnPostDn dn = JSON.parseObject(data, ZflWmsDnPostDn.class);
//        ZflWmsDnPostSerialno serial = JSON.parseObject(data, ZflWmsDnPostSerialno.class);

        AjaxResult ajaxResult = SAPConn.getDNPOSTXml(dn);


        if (Integer.parseInt(String.valueOf(ajaxResult.get("code"))) == 500) {
            ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();
            zflWmsDnPostDn.setVBELN(dn.get(0).getVBELN());
//
//            zflWmsDnPostDn.setStatus("2");
            zflWmsDnPostDn.setError(String.valueOf(ajaxResult.get("msg")));
            zflWmsDnPostDnService.updateZflWmsDnPostDnStatus(zflWmsDnPostDn);
        } else {


            List<ZflWmsDnPostRef> dnlist = (List<ZflWmsDnPostRef>) getAjaxResult(ajaxResult, ZflWmsDnPostRef.class, "ET_DN_REF", "ZFL_WMS_DN_POST").get("data");
            //  内向交货单
            if (dnlist != null && dnlist.size() != 0) {


                for (ZflWmsDnPostDn zflWmsDnPostDn : dn) {

                    for (ZflWmsDnPostRef zflWmsDnPostRef : dnlist) {
                        if (zflWmsDnPostDn.getPOSNR().equals(zflWmsDnPostRef.getVBELP())) {
                            ZflWmsDnPostDn zflWmsDnPostDn1 = new ZflWmsDnPostDn();
                            zflWmsDnPostDn1.setVBELN(zflWmsDnPostDn.getVBELN());
                            zflWmsDnPostDn1.setZEILE(zflWmsDnPostDn.getZEILE());
                            zflWmsDnPostDn1.setStatus("1");
                            zflWmsDnPostDn1.setPIKMG(zflWmsDnPostDn.getLFIMG());
                            zflWmsDnPostDn1.setError("成功过账");
                            zflWmsDnPostDn1.setMBLNR(zflWmsDnPostRef.getMBLNR());
                            zflWmsDnPostDn1.setVBELP(zflWmsDnPostRef.getVBELP());
                            zflWmsDnPostDn1.setMJAHR(zflWmsDnPostRef.getMJAHR());
                            zflWmsDnPostDn1.setPRUEFLOS(zflWmsDnPostRef.getPRUEFLOS());
                            zflWmsDnPostDn1.setTYPE("0");
                            zflWmsDnPostDnService.updateZflWmsDnPostDn(zflWmsDnPostDn1);
                        } else {

                            zflWmsDnPostDn.setPIKMG(zflWmsDnPostDn.getLFIMG());
                            zflWmsDnPostDn.setStatus("1");
                            zflWmsDnPostDn.setError("成功过账");
                            zflWmsDnPostDnService.updateZflWmsDnPostDn(zflWmsDnPostDn);
                        }
                    }


                }
            } else {
                //外向交货单

                ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();
                zflWmsDnPostDn.setVBELN(dn.get(0).getVBELN());
                zflWmsDnPostDn.setStatus("1");
                zflWmsDnPostDn.setError("成功过账");
                zflWmsDnPostDnService.updateZflWmsDnPostDnStatus(zflWmsDnPostDn);

            }

        }

//        List<ZflWmsDnPostDn> zflWmsDnPostDns = (List<ZflWmsDnPostDn>) getAjaxResult(ajaxResult, ZflWmsDnPostDn.class, "IT_DN", "ZFL_WMS_DN_POST").get("data");
//        zflWmsDnPostDns.forEach(z -> zflWmsDnPostDnController.addSave(z));
//
//        List<ZflWmsDnPostSerialno> zflWmsDnPostSerialnos = (List<ZflWmsDnPostSerialno>) getAjaxResult(ajaxResult, ZflWmsDnPostSerialno.class, "IT_SERIAL", "ZFL_WMS_DN_POST").get("data");
//        zflWmsDnPostSerialnos.forEach(z -> zflWmsDnPostSerialnoController.addSave(z));


    }

    /**
     * 生产订单
     *
     * @param AUFNR
     * @param GLTRP_LOW
     * @param GLTRP_HIG
     * @param REPGET
     * @param WERKS
     * @param DATES_START
     * @param DATES_END
     * @param VERID
     * @param OBJID
     * @return
     */
    @RequestMapping("/YPP_RFC_GET_WMS_PRO_LIST")
    @Log(title = "生产订单获取", businessType = BusinessType.INSERT)
    @ResponseBody
    public AjaxResult getProData(String AUFNR, String GLTRP_LOW, String GLTRP_HIG, String REPGET
            , String WERKS, String DATES_START, String DATES_END, String VERID, String OBJID) {
        AjaxResult ajaxResult = SAPConn.getProListXml("ZFL_WMS_GET_MO_LIST",
                new InputData("PC_I_AUFNR", AUFNR), new InputData("PD_I_GLTRP_LOW", GLTRP_LOW),
                new InputData("PD_I_GLTRP_HIG", GLTRP_HIG), new InputData("PC_I_REPGET", REPGET),
                new InputData("PC_I_WERKS", WERKS), new InputData("IN_DATES_START", DATES_START),
                new InputData("IN_DATES_END", DATES_END), new InputData("I_VERID", VERID),
                new InputData("I_OBJID", OBJID));
        AjaxResult ajaxResult1 = new AjaxResult();
        if (Integer.parseInt(String.valueOf(ajaxResult.get("code"))) == 500) {
            return ajaxResult1.put("msg", "同步未成功" + "," + ajaxResult.get("msg"));

        } else {
            List<YppRfcGetWmsProListDnlist> yppRfcGetWmsProListDnlists = (List<YppRfcGetWmsProListDnlist>) getAjaxResult(ajaxResult, YppRfcGetWmsProListDnlist.class, "PT_O_DNLIST", "ZFL_WMS_GET_MO_LIST").get("data");
            if (yppRfcGetWmsProListDnlists != null) {
                for (YppRfcGetWmsProListDnlist yppRfcGetWmsProListDnlist : yppRfcGetWmsProListDnlists) {
                    yppRfcGetWmsProListDnlistService.insertYppRfcGetWmsProListDnlist(yppRfcGetWmsProListDnlist);
                }
            }
            //信息都在这个表里
            List<YppRfcGetWmsProListPrdlist> yppRfcGetWmsProListPrdlists = (List<YppRfcGetWmsProListPrdlist>) getAjaxResult(ajaxResult, YppRfcGetWmsProListPrdlist.class, "PT_O_PRDLIST", "ZFL_WMS_GET_MO_LIST").get("data");
            if (yppRfcGetWmsProListPrdlists != null) {
                for (YppRfcGetWmsProListPrdlist yppRfcGetWmsProListPrdlist : yppRfcGetWmsProListPrdlists) {
                    if ("2240".equals(yppRfcGetWmsProListPrdlist.getWERKS())) {
                        yppRfcGetWmsProListPrdlistService.insertYppRfcGetWmsProListPrdlist(yppRfcGetWmsProListPrdlist);
                        sapService.insertInstockin(yppRfcGetWmsProListPrdlist);
                    }
                }
            }
            List<YppRfcGetWmsProListProdtext> yppRfcGetWmsProListProdtexts = (List<YppRfcGetWmsProListProdtext>) getAjaxResult(ajaxResult, YppRfcGetWmsProListProdtext.class, "PT_O_PRODTEXT", "ZFL_WMS_GET_MO_LIST").get("data");
            if (yppRfcGetWmsProListProdtexts != null) {
                for (YppRfcGetWmsProListProdtext yppRfcGetWmsProListProdtext : yppRfcGetWmsProListProdtexts) {
                    yppRfcGetWmsProListProdtextService.insertYppRfcGetWmsProListProdtext(yppRfcGetWmsProListProdtext);
                }
            }
        }
        return AjaxResult.success();
    }

    /**
     * 生产订单入库
     *
     * @return
     * @throws ParseException
     */
    @RequestMapping("/YPP_RFC_POST_TMDATA")
    @ResponseBody
    public void getTMDATAData(YppRfcPostTmdataTminterf yppRfcPostTmdataTminterf) {
        if(yppRfcPostTmdataTminterf !=null) {
            yppRfcPostTmdataTminterf.setZDOCTP("A");
            yppRfcPostTmdataTminterf.setZBUSTP("101");
            yppRfcPostTmdataTminterf.setWERKS("2240");
            yppRfcPostTmdataTminterf.setBUDAT(new SimpleDateFormat("yyyyMMdd").format(new Date()));

            YppRfcPostTmdataSerial yppRfcPostTmdataSerial = yppRfcPostTmdataSerialService.selectYppRfcPostTmdataSerialById(yppRfcPostTmdataTminterf.getZDOCNO());
            if (yppRfcPostTmdataSerial !=null) {
                AjaxResult ajaxResult = SAPConn.getTMADATAXml(yppRfcPostTmdataTminterf, yppRfcPostTmdataSerial);
                if (Integer.parseInt(String.valueOf(ajaxResult.get("code"))) == 500) {
                    //回传失败
                    YppRfcPostTmdataTminterf yppRfcPostTmdataTminterf1 = new YppRfcPostTmdataTminterf();
                    yppRfcPostTmdataTminterf1.setError(ajaxResult.get("msg").toString());
                    yppRfcPostTmdataTminterf1.setZDOCNO(yppRfcPostTmdataTminterf.getZDOCNO());
                    yppRfcPostTmdataTminterfService.updateYppRfcPostTmdataTminterf(yppRfcPostTmdataTminterf1);
                } else {
                    //回传成功
                    YppRfcPostTmdataTminterf yppRfcPostTmdataTminterf1 = new YppRfcPostTmdataTminterf();
                    yppRfcPostTmdataTminterf1.setStatus("1");
                    yppRfcPostTmdataTminterf1.setError("过账成功");
                    yppRfcPostTmdataTminterf1.setZDOCNO(yppRfcPostTmdataTminterf.getZDOCNO());
                    yppRfcPostTmdataTminterfService.updateYppRfcPostTmdataTminterf(yppRfcPostTmdataTminterf1);
                }
//        List<YppRfcPostTmdataReturndata> yppRfcPostTmdataReturndatas = (List<YppRfcPostTmdataReturndata>) getAjaxResult(ajaxResult, YppRfcPostTmdataReturndata.class, "ZTT_RETURNDATA", "YPP_RFC_POST_TMDATA").get("data");
//        for (YppRfcPostTmdataReturndata yppRfcPostTmdataReturndata : yppRfcPostTmdataReturndatas) {
//            yppRfcPostTmdataReturndataService.insertYppRfcPostTmdataReturndata(yppRfcPostTmdataReturndata);
//        }
//        List<YppRfcPostTmdataSerialarea> yppRfcPostTmdataSerialareas = (List<YppRfcPostTmdataSerialarea>) getAjaxResult(ajaxResult, YppRfcPostTmdataSerialarea.class, "ZTT_SERIALAREA", "YPP_RFC_POST_TMDATA").get("data");
//        for (YppRfcPostTmdataSerialarea yppRfcPostTmdataSerialarea : yppRfcPostTmdataSerialareas) {
//            yppRfcPostTmdataSerialareaService.insertYppRfcPostTmdataSerialarea(yppRfcPostTmdataSerialarea);
//        }
            }
        }
    }

    private AjaxResult getAjaxResult(AjaxResult ajaxResult, Class clazz, String table, String type) {
        if (Integer.parseInt(String.valueOf(ajaxResult.get("code"))) == 0) {
            try {
                List zflWmsGetMaterials = XmlUtil.resolveXmlToObjectList((String) ajaxResult.get("msg"), clazz, table, type);
                ajaxResult = AjaxResult.success(zflWmsGetMaterials);
            } catch (DocumentException | NoSuchFieldException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
                e.printStackTrace();
                ajaxResult = AjaxResult.error(e.getMessage());
            }
        }
        return ajaxResult;
    }


}
